const Router = require('koa-router');
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
const tool = require("../../tools/tool");
router.post('/app/xsdetail', async ctx => {
    let tenantId= ctx.user.tenantId
    let body= ctx.request.body
    let billNo = body.billNo
    let invoice = await db.query(`select * from v_main_withhx where tenantId=${tenantId} and billNo='${billNo}' `, {type: QueryTypes.SELECT, plain: true})
    let infos = await db.query(`select * from v_main_info where tenantId=${tenantId} and iid =${invoice.id} `, {type: QueryTypes.SELECT})
    invoice.infos = infos

    invoice.billStateName = invoice.billStateName === '草稿'?'待审核':invoice.billStateName
    invoice.amount = tool.xe.toFixed(invoice.amount, 2)
    invoice.skState = '未收款'
    if(tool.xe.toNumber(invoice.printNo) !== 0) {
        invoice.printState = '已打印'
    }
    else {
        invoice.printState = '未打印'
    }
    if(tool.xe.toNumber(invoice.leftYe) <tool.xe.toNumber(invoice.amount) && tool.xe.toNumber(invoice.leftYe)!==0) invoice.skState = `部分收款(${tool.xe.toFixed(invoice.hxAmount,2)})`
    if(tool.xe.toNumber(invoice.leftYe) == 0) invoice.skState = '已收款'

    return ctx.suc('', invoice)
})


module.exports = router;